package com.situ.tos2024.api;

import com.github.pagehelper.PageInfo;
import com.situ.tos2024.model.Order;
import com.situ.tos2024.service.OrderService;
import com.situ.tos2024.util.JsonResult;
import com.situ.tos2024.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/orders", produces = "application/json")
public class OrderApi {
    private OrderService orderService;

    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping
    public JsonResult list(@RequestParam(defaultValue = "1", name = "page") Integer pageNo,
                           @RequestParam(defaultValue = "20", name = "limit") Integer pageSize,
                           Order search) {
        //创建一个分页对象
        PaginateInfo pi = PaginateInfo.of(pageNo, pageSize);

        //分页查询订单
        List<Order> users = orderService.findAll(pi, search);

        //PageHelper的分页对象
        PageInfo<?> pif = pi.getPageInfo();

        return JsonResult.success(pif.getTotal(), users);
    }
}
